<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Crypt;
use Mail;
use DB;

class ForgetController extends Controller
{
   	public function forget()
   	{
   		return view('home.login.forget');
   	}

   	public function sendEmail(Request $request)
   	{
   		// dd($request -> all());
   		$email = $request -> email;
         // dd($email);
         $data = DB::table('users') -> where('email', $email) -> get();
   		$num = $data[0] -> remember_token;
   		Mail::raw($num, function($message) use($num)
         {
            $message -> subject('找回密码');
            $message -> to('1158125340@qq.com');
         });

         return view('home.login.edit', ['email' => $email]);

   	}

      public function yanzheng(Request $request)
      {
         // dd($request -> all());
         $email = $request -> email;
         // dd($email);
         $data = DB::table('users') -> whereIn('email', [$email]) -> get();
         // dd($data);
         if($data)
         {
            return view('home.login.send');
         }else
         {
            return back();
         }
      }

      public function edit(Request $request)
      {
         $data = $request -> except('_token');
         // $res = DB::table('users') -> where('remember_token', $data->remember_token) -> get();
         // dd($data['password']);
         $data['password'] = Crypt::encrypt($data['password']);
         $res = DB::table('users') -> where('email', $data['email']) -> update(['password' => $data['password']]);

         if($res)
         {
            return view('home.login.login');
         }
      }
}
